{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch as t\n",
    "import sys\n",
    "sys.path.append('../')\n",
    "from utils import get_score\n",
    "import glob\n",
    "import torch as t \n",
    "import numpy as np\n",
    "import json\n",
    "import time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "label_path =   '/home/a/code/pytorch/zhihu/ddd/labels.json'\n",
    "test_data_path='/home/a/code/pytorch/zhihu/ddd/val.npz' \n",
    "index2qid = np.load(test_data_path)['index2qid'].item()\n",
    "with open(label_path) as f: \n",
    "      labels_info = json.load(f)\n",
    "qid2label = labels_info['d']\n",
    "label2qid = labels_info['id2label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/LSTMText0.4119_word_val.pth\n",
      "../checkpoints/result/RCNN_0.4037_char_val.pth\n",
      "../checkpoints/result/LSTMText0.4031_char_val.pth\n",
      "../checkpoints/result/RCNN_0.4115_word_val.pth\n",
      "../checkpoints/result/DeepText0.4103_word_val.pth\n",
      "../checkpoints/result/FastText3_word_0.409160_val.pth\n",
      "../checkpoints/result/CNNText_tmp0.4109_word_val.pth\n",
      "../checkpoints/result/CNNText_tmp0.4024_char_val.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/*val.pth')\n",
    "r2=0\n",
    "for file in files:\n",
    "    if 'MultiModel' not in file:\n",
    "        print file\n",
    "        r2+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "true_labels = [qid2label[index2qid[2999967-200000+ii]] for ii in range(len(r2))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/DeepText_word_0.410051_aug_val.pth\n",
      "../checkpoints/result/tmp/RCNN0.41344_aug_word_val.pth\n",
      "../checkpoints/result/tmp/FastText3_word_weight1_0.40853_val.pth\n",
      "../checkpoints/result/tmp/inception0.41254_aug_word_val.pth\n",
      "../checkpoints/result/tmp/MultiCNNTextBNDeep_aug_char_0.38738_val.pth\n",
      "../checkpoints/result/tmp/RCNN0.39854_aug_char_val.pth\n",
      "../checkpoints/result/tmp/LSTMText0.41368_aug_word_val.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_aug=0\n",
    "for file in files:\n",
    "    if 'MultiModel' not in file and 'weight5' not in file:\n",
    "        print file\n",
    "        r_aug+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/MultiModelallfast2_419245894992_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModelallfast_0.419088_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.4171859_val.pth\n",
      "../checkpoints/result/tmp/MultiModel2_0.42560_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.42169_val.pth\n",
      "../checkpoints/result/tmp/MultiModelall_0.4235_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel2w2c_0.4213_val.pth\n",
      "../checkpoints/result/tmp/MultiModelallfast_0.41652_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.41987_word_val.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_multi=0\n",
    "for file in files:\n",
    "    if 'MultiModel' in file :\n",
    "        print file\n",
    "        r_multi+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/MultiCNNTextBNDeep_word_weight5_0.409409_val.pth\n",
      "../checkpoints/result/tmp/LSTMText_0.41230_weight5_val.pth\n",
      "../checkpoints/result/tmp/RCNN_0.41226_weight5_val.pth\n",
      "../checkpoints/result/tmp/inception_0.409912_weight5_val.pth\n",
      "../checkpoints/result/tmp/FastText3_word_weight5_0.407992_val.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_5=0\n",
    "for file in files:\n",
    "    if 'weight5' in file :\n",
    "        print file\n",
    "        r_5+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = (r2+r_aug*1.15+r_multi*13+r_5*0.15)\n",
    "result=(tmp).topk(5,1)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = tmp-t.load('../checkpoints/result/DeepText0.4103_word_val.pth')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = tmp+t.load('../checkpoints/result/DeepText0.4103_word_val.pth')*0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = tmp-t.load('../checkpoints/result/tmp/LSTMText0.41368_aug_word_val.pth')*0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = tmp-t.load('../checkpoints/result/CNNText_tmp0.4024_char_val.pth')*0.05"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = tmp-t.load('../checkpoints/result/tmp/MultiCNNTextBNDeep_word_weight5_0.409409_val.pth')*0.009"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "result=(tmp-t.load('../checkpoints/result/tmp/RCNN_0.41226_weight5_val.pth')*0.005).topk(5,1)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.4308092444404508, [118557, 66940, 43462, 30030, 22233])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,ss = get_score(predict_label_and_marked_label_list)\n",
    "score,ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[118549, 66953, 43459, 30022, 22233]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/MultiModelallfast_0.419088_aug_val.pth\n",
      "0.430803271596 [118566, 66917, 43472, 30030, 22232]\n",
      "../checkpoints/result/tmp/MultiModel_0.4171859_val.pth\n",
      "0.430798090542 [118551, 66936, 43483, 30012, 22233]\n",
      "../checkpoints/result/tmp/MultiModel2_0.42560_val.pth\n",
      "0.430789062942 [118553, 66954, 43454, 30016, 22230]\n",
      "../checkpoints/result/tmp/MultiModel_0.42169_val.pth\n",
      "0.430782420815 [118557, 66938, 43461, 30030, 22216]\n",
      "../checkpoints/result/tmp/MultiModelall_0.4235_aug_val.pth\n",
      "0.430802751153 [118561, 66931, 43463, 30026, 22236]\n",
      "../checkpoints/result/tmp/MultiModel2w2c_0.4213_val.pth\n",
      "0.430788528996 [118557, 66941, 43467, 30028, 22213]\n",
      "../checkpoints/result/tmp/MultiModelallfast_0.41652_aug_val.pth\n",
      "0.430795629655 [118545, 66939, 43463, 30040, 22228]\n",
      "../checkpoints/result/tmp/MultiModel_0.41987_word_val.pth\n",
      "0.430785328646 [118550, 66946, 43470, 30021, 22218]\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "for file in files:\n",
    "    if 'MultiModel' in file :\n",
    "        print file\n",
    "        result=(tmp+t.load(file)*0.5).topk(5,1)[1]\n",
    "        predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "        score,_,_,ss = get_score(predict_label_and_marked_label_list)\n",
    "        print score,ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# tmp = r_multi*9+t.load('../checkpoints/result/tmp/LSTMText0.41368_aug_word_val.pth')\\\n",
    "#          +t.load('../checkpoints/result/RCNN_0.4037_char_val.pth')\\\n",
    "#           +t.load('../checkpoints/result/tmp/FastText3_word_weight1_0.40853_val.pth')\\\n",
    "#           +t.load('../checkpoints/result/tmp/inception0.41254_aug_word_val.pth')\\\n",
    "#           +t.load('../checkpoints/result/LSTMText0.4031_char_val.pth')\\\n",
    "#           +t.load('../checkpoints/result/tmp/RCNN0.41344_aug_word_val.pth')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = (tmp+t.load('../checkpoints/result/tmp/FastText3_word_weight1_0.40853_val.pth')).topk(5,1)[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "blacklist:\n",
    "- ../checkpoints/result/tmp/FastText3_word_weight1_0.40853_val.pth\n",
    "- ../checkpoints/result/CNNText_tmp0.4109_word_val.pth\n",
    "- ../checkpoints/result/tmp/RCNN0.39854_aug_char_val.pth\n",
    "- smg\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "    rows = range(result.size(0))\n",
    "    for ii,item in enumerate(result):\n",
    "        rows[ii] = [index2qid[ii]] + [label2qid[str(_)] for _ in item ]\n",
    "    import csv\n",
    "    with open('result0.42887320426968284.csv','w') as f:\n",
    "        writer = csv.writer(f)\n",
    "        writer.writerows(rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "IOError",
     "evalue": "[Errno 2] No such file or directory: '../0811_1.csv'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIOError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-fda102fd4650>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mline_no\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m217360\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mrate\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0.003\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'../0811_1.csv'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m     \u001b[0mlines\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m','\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreadlines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0mnum\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrate\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mline_no\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mIOError\u001b[0m: [Errno 2] No such file or directory: '../0811_1.csv'"
     ]
    }
   ],
   "source": [
    "\n",
    "import csv\n",
    "line_no=217360\n",
    "rate = 0.003\n",
    "with open('../0811_1.csv') as f:\n",
    "    lines = [line.strip().split(',') for line in f.readlines()]\n",
    "    num = rate*line_no\n",
    "    stride = int(1/rate)\n",
    "    for line in lines[::stride]:\n",
    "        line[1:]=[-1,-1,-1,-1,-1]\n",
    "with open('../0811_1_end.csv','w') as f:\n",
    "    writer = csv.writer(f)\n",
    "    writer.writerows(lines)  \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['6215603645409872328',\n",
       " '4610596224687453206',\n",
       " '-7506384235581390893',\n",
       " '-6839713564940654454',\n",
       " '2919247920214845195',\n",
       " '-5932391056759866388\\r\\n']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lines[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.43381518936123437"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.429477037467622/0.99"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4285008976173052"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def target(args):\n",
    "        w1,w2,w3 = args\n",
    "        r = a + b*w1 +c*w2 + d*w3\n",
    "        result = r.topk(5,1)[1]\n",
    "        predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "        score,_,_,rrrrrrr = get_score(predict_label_and_marked_label_list)\n",
    "        print (args,score,_)#list_space = [hp.uniform('a',0,1),hp.uniform('b',0,1)]\n",
    "        return -rrrrrrr[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "d=results[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9034742719438373"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[-1].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4267781957592053"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.42655971790607233"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4253965699709525"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#coding:utf8\n",
    "\n",
    "\n",
    "label_path =   '/mnt/7/zhihu/ieee_zhihu_cup/data/labels.json'\n",
    "test_data_path='/mnt/7/zhihu/ieee_zhihu_cup/data/test.npz' \n",
    "def ensamble(file1,file2,label_path=label_path,     test_data_path=test_data_path,result_csv=None):\n",
    "\n",
    "    if result_csv is None:\n",
    "        \n",
    "        result_csv = time.strftime('%y%m%d_%H%M%S.csv')\n",
    "    a = t.load(file1)\n",
    "    b = t.load(file2)\n",
    "    r = 9.0*a+b\n",
    "    result = r.topk(5,1)[1]\n",
    "    \n",
    "    index2qid = np.load(test_data_path)['index2qid'].item()\n",
    "    with open(label_path) as f:   label2qid = json.load(f)['id2label']\n",
    "    rows = range(result.size(0))\n",
    "    for ii,item in enumerate(result):\n",
    "        rows[ii] = [index2qid[ii]] + [label2qid[str(_)] for _ in item ]\n",
    "    import csv\n",
    "    with open(result_csv,'w') as f:\n",
    "        writer = csv.writer(f)\n",
    "        writer.writerows(rows)\n",
    "    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    import fire\n",
    "    fire.Fire()\n",
    "\n",
    "files = ['CNNText_tmp0.4024_char_test.pth',\n",
    " 'CNNText_tmp0.4024_char_val.pth',\n",
    " 'DeepText0.4103_word_test.pth',\n",
    " 'Inception0.4110_word.pth',\n",
    " 'LSTMText0.4119_word.pth',\n",
    " 'LSTMText0.4031_char_test.pth',\n",
    " 'LSTMText0.4119_word_test.pth',\n",
    " 'DeepText0.4103_word_val.pth',\n",
    " 'CNNText_tmp0.4109_word_val.pth',\n",
    " 'LSTMText0.4119_word_val.pth',\n",
    " 'RCNN_0.4115_word_test.pth',\n",
    " 'RCNN_0.4037_char_val.pth',\n",
    " 'LSTMText0.4031_char_val.pth',\n",
    " 'RCNN_0.4115_word_val.pth',\n",
    " 'RCNN_0.4037_char_test.pth',\n",
    " 'CNNText_tmp0.4109_word_test.pth']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
